package com.ltg.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltg.admin.domain.entity.SysUser;
import com.ltg.admin.domain.param.LoginUser;
import com.ltg.admin.mapper.SysUserMapper;
import com.ltg.admin.param.LoginParam;
import com.ltg.admin.service.SysUserService;
import com.ltg.admin.utils.JwtUtil;
import com.ltg.admin.utils.Result;
import com.ltg.admin.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 登录
     *
     * @param loginParam
     * @return
     */
    @Override
    public Result login(LoginParam loginParam) {
        //进行用户认证。获取AuthenticationManager authenticate
        //获取认证对象
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginParam.getUsername(), loginParam.getPassword());
        //认证
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        //认证失败
        if (Objects.isNull(authenticate)) {
            throw new RuntimeException("登录失败");
        }
        //认证成功，生成token
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        String userId = loginUser.getSysUser().getId().toString();
        String token = JwtUtil.createToken(userId);
        //这边需要把token解析之后放在redis里面
        return Result.success(ResultCode.SUCCESS, token);
    }

    /**
     * 退出
     *
     * @return
     */
    @Override
    public Result loginOut() {
        //获取SecurityContextHolder中的id
        UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authenticationToken.getPrincipal();
        loginUser.getSysUser().getId();
        //删除redis的值
        return Result.success("退出成功");
    }

    @Override
    public Result<Page<SysUser>> queryUserList(Integer pageIndex, Integer pageSize) {
        Page<SysUser> sysUserPage = new Page<>(pageIndex, pageSize);
        Wrapper<SysUser> queryWrapper = new QueryWrapper<>();
        Page<SysUser> page = sysUserMapper.selectPage(sysUserPage, queryWrapper);
        return Result.success(page);
    }


}
